#include "Bullet.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;

char* bulletTexture[] = {
	"bullet01.png",
	"bullet02.png",
	"bullet03.png",
	"bullet04.png",
	"bullet05.png",
	"bullet06.png",
	"bullet07.png",
};

char* bulletSound[] = {
	"sound/bullet01.mp3",
	"sound/bullet02.mp3",
	"sound/bullet03.mp3",
	"sound/bullet04.mp3",
	"sound/bullet05.mp3",
	"sound/bullet06.mp3",
	"sound/bullet07.mp3",
};


Bullet* Bullet::create(BULLET_TYPE type, float bulletSpeed, float damage, OBJ_TYPE objType, float angle)
{
    Bullet *pRet = new Bullet();
    if (pRet && pRet->init()) {
        pRet->autorelease();
		pRet->d_type = type;
		pRet->d_angle = angle;
		pRet->d_bulletSpeed = bulletSpeed;
		pRet->d_damage = damage;
		pRet->d_ownerObjType = objType;

		pRet->initWithFile(bulletTexture[type]);

        return pRet;
    } else {
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}

void Bullet::fire()
{
	this->setRotation(d_angle*(-1));

	float vx = cos(d_angle * M_PI / 180) * d_bulletSpeed;
	float vy = sin(d_angle * M_PI / 180) * d_bulletSpeed;
	if(vy < 1 && vy > -1) {
		vy = 0;
	}
	CCAction* act = CCRepeatForever::create(CCMoveBy::create(1, ccp(vx, vy)));
	this->runAction(act);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(bulletSound[d_type]);
}

